package com.example.controller;

import com.example.common.Code;
import com.example.common.Result;
import com.example.domain.Employment;
import com.example.service.EmploymentService;
import com.example.utils.ExcelUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author luo
 * 应聘信息管理
 */
@RestController
@RequestMapping("/employments")
public class EmploymentController {
    @Autowired
    private EmploymentService employmentService;

    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping
    public Result getEmploy() {
        List<Employment> employments = employmentService.selectAllEmploy();
        String msg = employments != null ? "" : "数据设置失败，请重试！";
        Integer code = employments != null ? Code.GET_OK : Code.GET_ERR;
        return new Result(code, employments, msg);
    }

    @GetMapping("/{id}")
    public Result selectEmploymentById(@PathVariable Integer id) {
        Employment employments = null;

        String key = "Employment_ID：" +id;
        //先从redis中获取缓存数据
        employments = (Employment) redisTemplate.opsForValue().get(key);

        if(employments != null){
            //如果存在，直接返回，无需查询数据库
            return new Result(Code.GET_OK,employments,"查询成功！");
        }
        employments = employmentService.selectEmployById(id);
        redisTemplate.opsForValue().set(key,employments,60, TimeUnit.MINUTES);

        String msg = employments!=null ? "" : "数据查询失败，请重试！";
        Integer code = employments != null ? Code.GET_OK : Code.GET_ERR;
        return new Result(code,employments,msg);
    }
    @PostMapping("/ByName")
    public Result selectEmploymentByName(@RequestBody Employment employment){
        List<Employment> employments = employmentService.selectEmploymentByName(employment);
        String msg = employments!=null ? "" : "数据查询失败，请重试！";
        Integer code = employments != null ? Code.GET_OK : Code.GET_ERR;
        return new Result(code,employments,msg);
    }
    @PostMapping
    public Result insertEmployment(@RequestBody Employment employment) {
        int i = employmentService.addEmployment(employment);
        String msg = i > 0 ? "" : "数据设置失败，请重试！";
        if (i > 0) {
            System.out.println("插入成功");
            return new Result(Code.INSERT_OK, i, msg);
        } else {
            System.out.println("插入失败");
            return new Result(Code.INSERT_ERR, i, msg);
        }
    }

    @PutMapping
    public Result updateEmployment(@RequestBody Employment employment) {
        int i = employmentService.updateEmployment(employment);
        String msg = i > 0 ? "" : "数据设置失败，请重试！";

        //删除redis中缓存
        String key = "Employment_ID：" +employment.getId();
        redisTemplate.delete(key);

        if (i > 0) {
            System.out.println("更新成功");
            return new Result(Code.UPDATE_OK, i, msg);
        } else {
            System.out.println("更新失败");
            return new Result(Code.UPDATE_ERR, i, msg);
        }
    }
    @DeleteMapping("/{id}")
    public Result deleteEmployment(@PathVariable Integer id){
        int i = employmentService.deleteEmployment(id);
        String msg = i > 0 ? "" : "数据设置失败，请重试！";

        //删除redis中缓存
        String key = "Employment_ID：" +id;
        redisTemplate.delete(key);

        if (i > 0) {
            System.out.println("删除成功");
            return new Result(Code.DELETE_OK, i, msg);
        } else {
            System.out.println("删除失败");
            return new Result(Code.DELETE_OK, i, msg);
        }
    }
    @GetMapping("/{page}/{size}")
    public Result findUserList(@PathVariable int page,@PathVariable int size) {
        PageInfo<Employment> page1 = employmentService.findPage(page, size);
        Integer code = page1 != null ? Code.GET_OK : Code.GET_ERR;
        String msg = page1 != null ? "" : "分页失败，请重试！";
        return new Result(code, page1, msg);
    }
    @RequestMapping("/makeExcel")
    public void makeExcel(HttpServletResponse response) throws Exception {
        //打印Excel表
        ExcelUtils.exportExcel(response, "employment.xlsx", employmentService.getOutputExcelProjectData());
    }
}
